package com.longjianghuasheng.logistics.business.freight

import android.os.Bundle
import android.view.View
import coil.load
import com.longjianghuasheng.logistics.BR
import com.longjianghuasheng.logistics.R
import com.longjianghuasheng.logistics.base.BaseFragment
import com.longjianghuasheng.logistics.business.freight.adapter.DriverCooperationAdapter
import com.longjianghuasheng.logistics.databinding.FragmentDriverCooperationBinding
import com.longjianghuasheng.logistics.entity.CarInfo
import com.longjianghuasheng.logistics.eventViewModel
import com.longjianghuasheng.logistics.ext.*
import me.hgj.jetpackmvvm.ext.view.invisible
import me.hgj.jetpackmvvm.ext.view.visible

/**
 * 司机合作
 */
class DriverCooperationFragment :
    BaseFragment<DriverCooperationViewModel, FragmentDriverCooperationBinding>() {
    private val cardFrontListener = { _: View ->
        selectPicture {

            mViewModel.card_front = it.realPath

            mDatabind.ivCardFront.run {
                visible()
                load(it.path)
            }


        }

    }
    private val cardBackListener = { _: View ->
        selectPicture {

            mViewModel.card_back = it.realPath

            mDatabind.ivCardBack.run {
                visible()
                load(it.path)
            }


        }

    }
    private val certificateListener = { _: View ->
        selectPicture {

            mViewModel.books = it.realPath

            mDatabind.ivCertificate.run {
                visible()
                load(it.path)

            }


        }

    }


    private val driverCooperationAdapter by lazy {
        DriverCooperationAdapter()
    }

    override fun initViewModelId(): Int = BR.driverCooperationViewModel

    override fun layoutId(): Int = R.layout.fragment_driver_cooperation

    override fun initView(savedInstanceState: Bundle?) {

        initRecycler()
        mDatabind.initListener()


    }

    private fun initRecycler() {

        driverCooperationAdapter.run {

            mDatabind.recyclerView.initLinerDecoration(adapter = this).setHasFixedSize(false)
            setNewInstance(MutableList(1) { CarInfo() })

            addChildClickViewIds(
                R.id.tv_delete,
            )

            setOnItemChildClickListener { _, view, position ->

                when (view.id) {
                    R.id.tv_delete -> removeAt(position)
                    else -> {}


                }

            }
            onBooksListener = { view, position ->


                selectPicture {

                    data[position].books = it.realPath

                    view.load(it.path)


                }
            }
        }


    }


    private fun FragmentDriverCooperationBinding.initListener() {

        stvCompany.setOnSuperTextViewClickListener {

            mViewModel.team_work_list()

        }

        ivCardFront.setOnClickListener(cardFrontListener)
        ivClickLeft.setOnClickListener(cardFrontListener)
        tvDriverLicensePositive.setOnClickListener(cardFrontListener)

        ivCardBack.setOnClickListener(cardBackListener)
        ivClickRight.setOnClickListener(cardBackListener)
        tvDriverLicenseBack.setOnClickListener(cardBackListener)


        ivCertificate.setOnClickListener(certificateListener)
        tvCertificate.setOnClickListener(certificateListener)
        ivClickCertificate.setOnClickListener(certificateListener)

        btGetCode.setOnClickListener {

            mViewModel.mobile = stvPhone.centerEditValue

            mViewModel.onGetCode()

        }
        btnCommit.onClickIsLogin {


            mViewModel.run {


                names = stvName.centerEditValue
                mobile = stvPhone.centerEditValue
                code = stvCode.centerEditValue
                cars_info.clear()
                cars_info.addAll(driverCooperationAdapter.data)
                driver_work()
            }
        }

        val addListener = { _: View ->

            driverCooperationAdapter.addData(CarInfo())


        }
        addCar.setOnClickListener(addListener)
        tvAdd.setOnClickListener(addListener)
    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.notify.observer(this) {
            mDatabind.clear()
            eventViewModel.onGoBackToMyEvent.value = 0
        }

        mViewModel.result.observer(this) {
            showPicker(options = it.map { it.names ?: "" }.toTypedArray()) { str ->

                mViewModel.team_id = it.first { it.names == str }.id ?: 0
                mDatabind.stvCompany.setLeftString(str)

            }

        }
    }

    private fun FragmentDriverCooperationBinding.clear() {
        stvName.setCenterEditString("")
        stvPhone.setCenterEditString("")
        stvCode.setCenterEditString("")

        mViewModel.run {
            card_front = ""
            card_back = ""
            books = ""
        }
        ivCardFront.invisible()
        ivCardBack.invisible()
        ivCertificate.invisible()


        driverCooperationAdapter.setNewInstance(MutableList(1) { CarInfo() })

    }

    override fun initData() {
        super.initData()
    }

    override fun lazyLoadData() {
        super.lazyLoadData()
    }

}